import {getItemByItemId,addItemToCollect,checkInCollect,deleteItemfromCollect} from "@/api/api";
import {addCommentToItem, addItemToCart, getAllCommentByItemId, updateCart} from "../../../src/api/api";
let nowUsername= sessionStorage.getItem("nowUsername");



export default {
  data() {
    return {
      quantity: 1,
      itemId: "",
      productId: "",
      catId: "",
      flag:0, //用来判断从哪里打开的viewItem页面
      heartimg: "static/picture/heart1.png",
      username: "h",
      commentNum: 0,
      item: {

      },
      commentList:[

      ],
      myComment:{
        itemId:this.$route.query.itemId,
        username:"h",
        content:"",
        commentTime: ""
      }
    }
  },
  mounted() {
    this.itemId = this.$route.query.itemId;
    this.productId = this.$route.query.productId;
    this.catId = this.$route.query.catId;
    this.flag = this.$route.query.flag;
    this.getItem();
    this.getCommentList();

    var date=new Date();
    var dateTime=date.getTime();
    this.myComment.commentTime = this.formatDate(dateTime);

    let nowUsername = sessionStorage.getItem("nowUsername");
    checkInCollect(nowUsername,this.itemId).then((res) =>{
     console.log("打印预加载"+res);
      if(res.data.status==3){
            this.heartimg="static/picture/heart2.png";
      }else{
        this.heartimg="static/picture/heart1.png";
      }
    }).catch((res) => {
      alert("错误1：" + res);
    });

  },
  methods:{
    getItem(){
      console.log(this.itemId);
      getItemByItemId(this.$route.query.itemId).then((res) =>{
        this.item.catId = res.data.data.categoryId;
        this.item.productId =  res.data.data.productId;
        this.item.itemId =  res.data.data.itemId;
        this.item.name =  res.data.data.iName;
        this.item.oldPrice =  res.data.data.listPrice + res.data.data.unitCost;
        this.item.nowPrice = res.data.data.listPrice;
        this.item.descn = res.data.data.iDescn;
        this.item.image = res.data.data.iImage;
        })
    },
    //拿到所有的评论
    getCommentList(){
      getAllCommentByItemId(this.$route.query.itemId).then((res)=>{
        console.log("拿到所有评论");

        this.commentList = res.data.data;
        this.commentNum = this.commentList.length;
      })
    },
    addCart(){
      let param = new FormData;
      param.append("increment", this.quantity);
      // console.log("加入购物车");
      console.log(nowUsername);
      addItemToCart(nowUsername,this.itemId,param).then((res) => {
        this.getItem();
        alert("Added to shopping cart successfully.")
        console.log(res);

      }).catch((res) => {
        alert("错误：" + res);
      });
    },
    //增加一个评论
    addComment(){
      console.log(this.myComment);
      addCommentToItem(this.myComment).then((res)=>{
        alert("Comment successfully.");
      })
    },
    addWish(itemId) {
      let nowUsername= sessionStorage.getItem("nowUsername");
      console.log(itemId);
      if(nowUsername!==null){
      checkInCollect(nowUsername,itemId).then((res) =>{
        console.log(res);
         if(res.data.status==3){
           deleteItemfromCollect(nowUsername,itemId).then((res) =>{
             console.log(nowUsername);
             if (res.data.status == 0) {
               alert("商品已取消收藏");
               this.heartimg="static/picture/heart1.png";
             }
           });

         }else{
           addItemToCollect(nowUsername,itemId).then((res) => {
             console.log(res);
             if (res.data.status == 0) {
               alert("添加成功");
               this.heartimg="static/picture/heart2.png";
             } else if (res.data.status == 2) {
               alert("没有权限，请检查网络");
             }
           }).catch((res) => {
             // alert("错误3：" + res);
           });

         }

      }).catch((res) => {
        alert("错误2：" + res);
      });
      }else{
        alert("请先登录");
      }
    },
    // 时间戳格式化
    formatDate(datetime) {
      var date = new Date(datetime);// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var year = date.getFullYear();
      var month = ('0' + (date.getMonth() + 1)).slice(-2);
      var sdate = ('0' + date.getDate()).slice(-2);
      var hour = ('0' + date.getHours()).slice(-2);
      var minute = ('0' + date.getMinutes()).slice(-2);
      var second = ('0' + date.getSeconds()).slice(-2);
      // 拼接
      var result = year + '-' + month + '-' + sdate + ' ' + hour + ':' + minute + ':' + second;
      // 返回
      return result;
    },
  }
}
